/**
 * @file 算出两个点之间的最小花费
 * @author
 */

var node0 = {
    name: 'node0',
    relations: [],
    value: Infinity
};

var node1 = {
    name: 'node1',
    relations: [],
    value: Infinity
};

var node2 = {
    name: 'node2',
    relations: [],
    value: Infinity
};

var node3 = {
    name: 'node3',
    relations: [],
    value: Infinity
};

var node4 = {
    name: 'node4',
    relations: [],
    value: Infinity
};

node0.relations = {
    9: node1,
    2: node2,
    6: node4
};

node1.relations = {
    9: node0,
    3: node2
};

node2.relations = {
    2: node0,
    5: node3,
    3: node1
};

node3.relations = {
    5: node2,
    1: node4
}

node4.relations = {
    6: node0,
    1: node3
};

const search = (root) => {
    let nodeQueue = [root];
    root.value = 0;

    while (nodeQueue.length) {
        const curNode = nodeQueue.shift();
        for (let dis in curNode.relations) {
            if ((+dis) + curNode.value < curNode.relations[dis].value) {
                curNode.relations[dis].value = (+dis) + curNode.value;
                nodeQueue.push(curNode.relations[dis]);
            }
        }
    }
};

const start = () => {
    const root = node0;
    search(root);
    console.log('3-min:', node3.value);
};

start();